/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <baja/common.hpp>
#include <baja/math.hpp>
#include <baja/graphics.hpp>
#include <baja/observable.hpp>
#include <journal/views/interfaces.h>
#include <journal/views/camera.h>

namespace journal {  namespace view {

namespace b = baja;
namespace a = baja::app;
namespace m = baja::math;
namespace g = baja::graphics;

class light : public std::enable_shared_from_this<light>
{
public:
    light() {}

    static std::shared_ptr<light> create()
    {
        std::shared_ptr<light> returnValue = std::make_shared<light>();
        returnValue->init();
        return returnValue;
    }

    ~light() { this->cleanup(); }

public:
    std::shared_ptr<icamera> camera()
    {
        return _camera;
    }

    void apply(const std::shared_ptr<g::iengine>& graphics)
    {
        _light.position = _camera->position();
        _light.direction = _camera->direction();
        graphics->setLight(0, _light);
    }

private:

    void init()
    {
        _camera = std::dynamic_pointer_cast<view::icamera>(camera::create());

        std::memset(&_light, 0, sizeof(g::light));
        _light.type = g::light_type::directional;
        _light.color = constants::general::directionalLightColor();
        _light.specularIntensity = 1.0f;
        _light.position = _camera->position();
        _light.direction = _camera->direction();
    }

    void cleanup()
    {
        _camera = nullptr;
    }

private:
    std::shared_ptr<icamera> _camera;
    g::light _light;
};

}} // namespace journal::view